/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;


import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import model.Criterio;
import model.Escala;
import util.PropertiesIO;

/**
 *
 * @author jorge
 */
public class CriterioPanel extends javax.swing.JPanel {

    private ButtonGroup grupoBotonesOptimizar;
    private ButtonGroup grupoBotonesTipoMedida;
    /**
     * Creates new form CriterioPanel
     */
    public CriterioPanel() throws IOException {
        initComponents();
        inicializarComboEscalas();
        grupoBotonesOptimizar = new ButtonGroup();
        grupoBotonesOptimizar.add(botonMaximizar);
        grupoBotonesOptimizar.add(botonMinimizar);
        grupoBotonesTipoMedida = new ButtonGroup();
        grupoBotonesTipoMedida.add(botonCualitativo);
        grupoBotonesTipoMedida.add(botonCuantitativo);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    public void crearDatosCriterio(Criterio criterio){
        
        nombreText.setText(criterio.getNombre());
        descripcionText.setText(criterio.getDescripcion());
        unidadesText.setText(criterio.getUnidadMedida());
        
        if(criterio.getCuantitativoCualitativo().equals(Criterio.CUANTITATIVO)){
            botonCuantitativo.setSelected(true);
            unidadesText.setEnabled(true);
            jComboEscala.setEnabled(false);
        }else{  
            botonCualitativo.setSelected(true);
            unidadesText.setEnabled(false);
            jComboEscala.setEnabled(true); 
            propNombreEscalaText.setText(criterio.getEscalaMedida().getNombre());
            propDescripcionEscalaText.setText(criterio.getEscalaMedida().getDescripcion());
        }
        
        if(criterio.getOptimizacion().equals(Criterio.MAXIMIZAR)){
            botonMaximizar.setSelected(true);
        }else{
            botonMinimizar.setSelected(true);        
        }
        jComboEscala.setSelectedItem(criterio.getEscalaMedida());
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        nombreText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descripcionText = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        unidadesText = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        botonMaximizar = new javax.swing.JRadioButton();
        botonMinimizar = new javax.swing.JRadioButton();
        botonCualitativo = new javax.swing.JRadioButton();
        botonCuantitativo = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        propNombreEscalaText = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        guardarEscalaBoton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        propDescripcionEscalaText = new javax.swing.JTextArea();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jComboEscala = new javax.swing.JComboBox();

        setMaximumSize(new java.awt.Dimension(304, 262));
        setMinimumSize(new java.awt.Dimension(304, 262));

        jLabel1.setText("Nombre");

        nombreText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nombreTextActionPerformed(evt);
            }
        });

        jLabel2.setText("Descripción");

        descripcionText.setColumns(20);
        descripcionText.setLineWrap(true);
        descripcionText.setRows(5);
        jScrollPane1.setViewportView(descripcionText);

        jLabel3.setText("Unidades de medida");

        unidadesText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                unidadesTextActionPerformed(evt);
            }
        });

        jLabel6.setText("Escala de medida");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Opciones "));

        botonMaximizar.setText("Maximizar");
        botonMaximizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonMaximizarActionPerformed(evt);
            }
        });

        botonMinimizar.setText("Minimizar");

        botonCualitativo.setText("Cualitativo");
        botonCualitativo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCualitativoActionPerformed(evt);
            }
        });

        botonCuantitativo.setText("Cuantitativo");
        botonCuantitativo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCuantitativoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botonMaximizar)
                    .addComponent(botonCuantitativo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botonMinimizar)
                    .addComponent(botonCualitativo))
                .addGap(24, 24, 24))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonMaximizar)
                    .addComponent(botonMinimizar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonCuantitativo)
                    .addComponent(botonCualitativo))
                .addGap(0, 14, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Propiedades Escala"));

        jLabel4.setText("Nombre Escala");

        propNombreEscalaText.setEnabled(false);
        propNombreEscalaText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                propNombreEscalaTextActionPerformed(evt);
            }
        });

        jLabel5.setText("Descripcion Escala");

        guardarEscalaBoton.setText("Guardar Escala");
        guardarEscalaBoton.setEnabled(false);
        guardarEscalaBoton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarEscalaBotonActionPerformed(evt);
            }
        });

        propDescripcionEscalaText.setColumns(20);
        propDescripcionEscalaText.setLineWrap(true);
        propDescripcionEscalaText.setRows(5);
        propDescripcionEscalaText.setEnabled(false);
        propDescripcionEscalaText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                propDescripcionEscalaTextKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(propDescripcionEscalaText);

        jLabel7.setForeground(new java.awt.Color(153, 153, 153));
        jLabel7.setText("Cuantitativo-Cualitativo Ejemplo: ");

        jLabel8.setForeground(new java.awt.Color(153, 153, 153));
        jLabel8.setText("Ejemplo: EscalaEjemplo");

        jLabel9.setForeground(new java.awt.Color(153, 153, 153));
        jLabel9.setText("(1:muy baja;3:baja;5:media;7:alta;9:muy alta)             ");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(guardarEscalaBoton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(propNombreEscalaText)))
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7)
                            .addComponent(jLabel9))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(propNombreEscalaText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addGap(23, 23, 23)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(guardarEscalaBoton)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        jComboEscala.setEnabled(false);
        jComboEscala.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboEscalaItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addComponent(jLabel3))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(unidadesText)
                            .addComponent(jScrollPane1)
                            .addComponent(nombreText)
                            .addComponent(jComboEscala, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(nombreText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(3, 3, 3)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboEscala, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(unidadesText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))))
                .addContainerGap(66, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void nombreTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nombreTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nombreTextActionPerformed

    private void unidadesTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_unidadesTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_unidadesTextActionPerformed

    private void botonMaximizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonMaximizarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_botonMaximizarActionPerformed

    private void botonCuantitativoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCuantitativoActionPerformed
        if (botonCuantitativo.isSelected()) {
            unidadesText.setEnabled(true);
            jComboEscala.setEnabled(false);
            propDescripcionEscalaText.setEnabled(false);
            propNombreEscalaText.setEnabled(false);
            guardarEscalaBoton.setEnabled(false);
        } 
    }//GEN-LAST:event_botonCuantitativoActionPerformed

    private void botonCualitativoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCualitativoActionPerformed
        if (botonCualitativo.isSelected()) {
            unidadesText.setEnabled(false);
            jComboEscala.setEnabled(true);
            propDescripcionEscalaText.setEnabled(true);
            propNombreEscalaText.setEnabled(true);
            guardarEscalaBoton.setEnabled(false);
        } 
    }//GEN-LAST:event_botonCualitativoActionPerformed

    private void propNombreEscalaTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_propNombreEscalaTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_propNombreEscalaTextActionPerformed

    private void jComboEscalaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboEscalaItemStateChanged
        Escala temporal = (Escala) jComboEscala.getSelectedItem();
            if(botonCualitativo.isSelected()){
                propNombreEscalaText.setText(temporal.getNombre());
                propDescripcionEscalaText.setText(temporal.getDescripcion());
            }
    }//GEN-LAST:event_jComboEscalaItemStateChanged

    private void guardarEscalaBotonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarEscalaBotonActionPerformed
        try {
            File prueba = new File("src/properties/escalas.properties");
            Properties pCC = PropertiesIO.read(prueba);
            Enumeration keys = pCC.keys();
            pCC.put(propNombreEscalaText.getText(), propDescripcionEscalaText.getText());
            Escala escala = new Escala(propNombreEscalaText.getText(),propDescripcionEscalaText.getText());
            jComboEscala.addItem(escala);
            propNombreEscalaText.setText("");
            propDescripcionEscalaText.setText("");
            PropertiesIO.write(pCC, prueba);
            guardarEscalaBoton.setEnabled(false);
        } catch (IOException ex) {
            Logger.getLogger(CriterioPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_guardarEscalaBotonActionPerformed

    private void propDescripcionEscalaTextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_propDescripcionEscalaTextKeyPressed
        guardarEscalaBoton.setEnabled(true);
    }//GEN-LAST:event_propDescripcionEscalaTextKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton botonCualitativo;
    private javax.swing.JRadioButton botonCuantitativo;
    private javax.swing.JRadioButton botonMaximizar;
    private javax.swing.JRadioButton botonMinimizar;
    private javax.swing.JTextArea descripcionText;
    private javax.swing.JButton guardarEscalaBoton;
    private javax.swing.JComboBox jComboEscala;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField nombreText;
    private javax.swing.JTextArea propDescripcionEscalaText;
    private javax.swing.JTextField propNombreEscalaText;
    private javax.swing.JTextField unidadesText;
    // End of variables declaration//GEN-END:variables
    public String getNombre(){
        return nombreText.getText();
    }
    
    public String getDescripcion(){
        return descripcionText.getText();
    }

    public String getUnidades(){
        return unidadesText.getText();
    }
    
    public Escala getEscala(){
        return (Escala)jComboEscala.getSelectedItem();
    }
    
    public String getCualitativoCuantitativo(){
        if(botonCuantitativo.isSelected()){
            return Criterio.CUANTITATIVO;
        }else{
            return Criterio.CUALITATIVO;
        }
    }
    
    public String getOptimizacion(){
        if(botonMaximizar.isSelected()){
            return Criterio.MAXIMIZAR;
        }else{
            return Criterio.MINIMIZAR;
        }
    }

    private void inicializarComboEscalas() throws IOException {
        
        File prueba = new File("src/properties/escalas.properties");
        Properties pCC = PropertiesIO.read(prueba);
        Enumeration keys = pCC.keys();
        while(keys.hasMoreElements()){
            String key = (String) keys.nextElement();
            String value = pCC.getProperty(key);
            Escala escala = new Escala(key,value);
            jComboEscala.addItem(escala);
        }
    }
    
}
